/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.oa.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.websocket.service.system.SystemInfoSocketHandler;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.oa.entity.OaNotify;
import com.jeeplus.modules.oa.entity.OaNotifyRecord;
import com.jeeplus.modules.oa.entity.RandomDrawingHis;
import com.jeeplus.modules.oa.service.OaNotifyService;
import com.jeeplus.modules.oa.service.RandomDrawingHisService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 通知通告Controller
 *
 * @author jeeplus
 * @version 2017-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/randomDrawingHis")
public class RandomDrawingHisController extends BaseController {

    @Autowired
    private RandomDrawingHisService randomDrawingHisService;

    @ModelAttribute
    public RandomDrawingHis get(@RequestParam(required = false) String id) {
        RandomDrawingHis entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = randomDrawingHisService.get(id);
        }
        if (entity == null) {
            entity = new RandomDrawingHis();
        }
        return entity;
    }

    @RequiresPermissions("oa:randomDrawingHis:list")
    @RequestMapping(value = {"list", ""})
    public String list(OaNotify oaNotify, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("isSelf", false);
        return "modules/oa/randomDrawingHis/randomDrawingHisList";
    }

    /**
     * 通告列表数据
     */
    @ResponseBody
    @RequiresPermissions("oa:randomDrawingHis:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(RandomDrawingHis randomDrawingHis, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<RandomDrawingHis> page = randomDrawingHisService.findPage(new Page<RandomDrawingHis>(request, response), randomDrawingHis);
        return getBootstrapData(page);
    }

    /**
     * 查看，增加，编辑报告表单页面
     */
    @RequiresPermissions(value = {"oa:randomDrawingHis:view", "oa:randomDrawingHis:add", "oa:randomDrawingHis:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(RandomDrawingHis randomDrawingHis, boolean isSelf, Model model) {
        model.addAttribute("randomDrawingHis", randomDrawingHis);
        return "modules/oa/randomDrawingHis/randomDrawingHisForm";
    }

    @ResponseBody
    @RequiresPermissions(value = {"oa:randomDrawingHis:add", "oa:randomDrawingHis:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(RandomDrawingHis randomDrawingHis, Model model, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(randomDrawingHis);
        if (StringUtils.isNotBlank(errMsg)) {
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        randomDrawingHisService.save(randomDrawingHis);
        j.setMsg("保存成功");
        return j;
    }

    @ResponseBody
    @RequiresPermissions("oa:randomDrawingHis:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(RandomDrawingHis randomDrawingHis, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        randomDrawingHisService.delete(randomDrawingHis);
        j.setMsg("删除成功");
        return j;
    }

    @ResponseBody
    @RequiresPermissions("oa:randomDrawingHis:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        String idArray[] = ids.split(",");
        for (String id : idArray) {
            randomDrawingHisService.delete(randomDrawingHisService.get(id));
        }
        j.setMsg("删除通知成功");
        return j;
    }
}